﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.MathLib.CSG
{
   class Quantization
    {
        public const int BITS = 30;
        // MAGNIFY * RESHRINK == 1
        double MAGNIFY;
        double RESHRINK;

        public Quantization() { }

        // NOTE: none of these values should be modified by the clients

        public long magnify(double number)
        {
            return (long)(number * MAGNIFY);
        }
        public double reshrink(double number)
        {
            return RESHRINK * number;
        }
        public double quantize(double number)
        {
            return reshrink(magnify(number));
        }

        // given the specified number of bits,
        // and bound on the coordinate values of points,
        // fit as fine-grained a grid as possible over the space.
        public void callibrate(double maximumMagnitude)
        {
            int max_exponent;
            MathEx.Frexp(maximumMagnitude, out max_exponent);
            max_exponent++; // ensure that 2^max_exponent > maximumMagnitude

            // set constants
            MAGNIFY = Math.Pow(2.0, BITS - max_exponent);
            // we are guaranteed that maximumMagnitude * MAGNIFY < 2.0^BITS
            RESHRINK = Math.Pow(2.0, max_exponent - BITS);
        }

    }
}
